@extends('layouts.app')

@section('content')
    <h4>已有用户({{ $count }} 条)</h4>
    <hr>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th width="10">ID</th>
            <th width="80">用户名</th>
            <th width="150">角色</th>
            <th width="100">创建时间</th>
            <th width="100">操作</th>
        </tr>
        </thead>
        <tbody>
        @forelse($data as $record)
            <tr>
                <td>{{ $record->id }}</td>
                <td>{{ $record->name }}</td>
                <td>{{ $record->role == 1 ? '管理员' : '普通会员' }}</td>
                <td>{{ date('Y-m-d H:i:s', $record->created_at) }}</td>
                <td>
                    @if ($record->role != 1)
                    @if ($record->is_blocked == 1)
                        <button type="button"
                                data-block="1"
                                class="btn btn-danger block"
                                data-href="{{ route('add-blacklist', ['user_id' => $record->id]) }}">从黑名单中移除</button>
                    @else
                        <button type="button"
                                data-block="2"
                                class="btn btn-danger block"
                                data-href="{{ route('add-blacklist', ['user_id' => $record->id]) }}">加入黑名单</button>
                    @endif
                    @endif
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="100" class="text-center">暂无留言</td>
            </tr>
        @endforelse
        </tbody>
    </table>
    {{ $data->links() }}
@endsection
@section('scripts')
<script>
    let token = @json(csrf_token());

    $(function () {
        $('.block').click(function () {
            let msg = '确定要加入黑名单吗？';
            let result = '加入黑名单成功';
            if ($(this).data('block') == 1) {
                msg = '确定要从黑名单移除吗？';
                result = '移除黑名单成功';
            }

            if (confirm(msg)) {
                let route = $(this).data('href');
                $.ajax({
                    method: "POST",
                    url: route,
                    data: {_token: token},
                    success: function (res) {
                        if (res.status == 'error') {
                            alert(res.message);
                        } else {
                            alert(result);
                            location.reload();
                        }
                    }
                });
            }
        })
    });
</script>
@endsection

